﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Telerik.Windows.Controls.DataServices;
using DataServiceDataSource.WPF.Model.EF;
using Telerik.Windows.Data;
using System.Data.Services.Client;

namespace DataServiceDataSource.WPF.ViewModel
{
    public class MvvmViewModel : INotifyPropertyChanged
    {
        private readonly NorthwindEntities context;
        private readonly QueryableDataServiceCollectionView<Order> ordersView;
        private readonly QueryableDataServiceCollectionView<Order_Detail> orderDetailsView;
        private readonly FilterDescriptor orderDetailsFilter;

        private bool areOrderBusy;
        private bool areOrderDetailsBusy;
        private bool canUserSelectOrders;

        private Order selectorOrder;
        public Order SelectorOrder
        {
            get { return this.selectorOrder; }
            set
            {
                if (value != this.selectorOrder)
                {
                    this.selectorOrder = value;

                    if (this.selectorOrder != null)
                    {
                        this.orderDetailsFilter.Value = this.selectorOrder.OrderID;
                    }
                    else
                    {
                        this.orderDetailsFilter.Value = -1;
                    }
                }

                if (!this.orderDetailsView.AutoLoad)
                {
                    this.orderDetailsView.AutoLoad = true;
                }
            }
        }

        public IEnumerable<Order> Orders
        {
            get { return this.ordersView; }
        }

        public IEnumerable<Order_Detail> OrderDetails
        {
            get { return this.orderDetailsView; }
        }

        public bool AreOrdersBusy
        {
            get { return this.areOrderBusy; }
            set
            {
                if (this.areOrderBusy != value)
                {
                    this.areOrderBusy = value;
                    this.OnPropertyChanged("AreOrdersBusy");
                }
            }
        }

        public bool AreOrderDetailsBusy
        {
            get
            {
                return this.areOrderDetailsBusy;
            }
            set
            {
                if (this.areOrderDetailsBusy != value)
                {
                    this.areOrderDetailsBusy = value;
                    this.OnPropertyChanged("AreOrderDetailsBusy");
                }
            }
        }

        public bool CanUserSelectOrders
        {
            get
            {
                return this.canUserSelectOrders;
            }
            set
            {
                if (this.canUserSelectOrders != value)
                {
                    this.canUserSelectOrders = value;
                    this.OnPropertyChanged("CanUserSelectOrders");
                }
            }
        }

        public MvvmViewModel()
        {
            using (this.context = new NorthwindEntities())
            {
               
                this.ordersView.PropertyChanged += this.OnOrdersViewPropertyChanged;
            }
        }

        private void OnOrdersViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsBusy")
            {
                this.AreOrdersBusy = this.ordersView.IsBusy;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
